﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;

namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class SurveyQuestionQuestionGroupsController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        ISurveyQuestionQuestionGroupService surveyQuestionQuestionGroupService;

          public SurveyQuestionQuestionGroupsController(ISurveyQuestionQuestionGroupService surveyQuestionQuestionGroupService)
        {
            this.surveyQuestionQuestionGroupService = surveyQuestionQuestionGroupService;
        }

        public SurveyQuestionQuestionGroupsController()
        {
            
        }

        //
        // GET: /SurveyQuestionQuestionGroups/

        public ViewResult Index()
        {
            var surveyQuestionQuestionGroups = surveyQuestionQuestionGroupService.GetAllSurveyQuestionQuestionGroups();
            var viewModel = new SurveyQuestionQuestionGroupIndexViewModel() { SurveyQuetionQuestionGroups = surveyQuestionQuestionGroups };
            return View(viewModel);
        }

        //
        // GET: /SurveyQuestionQuestionGroups/Details/5

        public ViewResult Details(int id)
        {
            SurveyQuestionQuestionGroup surveyquestionquestiongroup = unitOfWork.SurveyQuestionQuestionGroupRepository.GetById(id);
            return View(surveyquestionquestiongroup);
        }

        //
        // GET: /SurveyQuestionQuestionGroups/Create

        public ActionResult Create()
        {
            ViewBag.QuestionGroupFK = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence ");
            ViewBag.SurveyQuestionFK = new SelectList(unitOfWork.SurveyQuestionRepository.Get(), "Id", "Text");
            return View();
        } 

        //
        // POST: /SurveyQuestionQuestionGroups/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")]  SurveyQuestionQuestionGroup surveyquestionquestiongroup)
        {
            if (ModelState.IsValid)
            {
                if (this.CheckList(surveyquestionquestiongroup).Count() == 0)
                {
                    unitOfWork.SurveyQuestionQuestionGroupRepository.Add(surveyquestionquestiongroup);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "This surveyQuestion - QuestionGroup combination already exists");
                }
            }

            ViewBag.QuestionGroupFK = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence", surveyquestionquestiongroup.QuestionGroupFK);
            ViewBag.SurveyQuestionFK = new SelectList(unitOfWork.SurveyQuestionRepository.Get(), "Id", "Text", surveyquestionquestiongroup.SurveyQuestionFK);
            return View(surveyquestionquestiongroup);
        }
        
        //
        // GET: /SurveyQuestionQuestionGroups/Edit/5
 
        public ActionResult Edit(int id)
        {
            SurveyQuestionQuestionGroup surveyquestionquestiongroup = unitOfWork.SurveyQuestionQuestionGroupRepository.GetById(id);
            ViewBag.QuestionGroupFK = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence ");
            ViewBag.SurveyQuestionFK = new SelectList(unitOfWork.SurveyQuestionRepository.Get(), "Id", "Text");
            return View(surveyquestionquestiongroup);
        }

        private List<SurveyQuestionQuestionGroup> CheckList(SurveyQuestionQuestionGroup surveyQuestionQuestionGroup)
        {
            List<SurveyQuestionQuestionGroup> checkList = new List<SurveyQuestionQuestionGroup>();
            IQueryable<SurveyQuestionQuestionGroup> list = unitOfWork.SurveyQuestionQuestionGroupRepository.GetAll();
            foreach (SurveyQuestionQuestionGroup uqqg in list)
            {
                if (uqqg.QuestionGroupFK == surveyQuestionQuestionGroup.QuestionGroupFK && uqqg.SurveyQuestionFK == surveyQuestionQuestionGroup.SurveyQuestionFK)
                {
                    checkList.Add(uqqg);// zou normaal max. 1 member mogen bevatten

                }
            }
            return checkList;
        }

        //
        // POST: /SurveyQuestionQuestionGroups/Edit/5

        [HttpPost]
        public ActionResult Edit(SurveyQuestionQuestionGroup surveyquestionquestiongroup)
        {
            if (ModelState.IsValid)
            {
                if (this.CheckList(surveyquestionquestiongroup).Count() == 0)
                {//checken of combinatie al bestaat!!
                    unitOfWork.SurveyQuestionQuestionGroupRepository.Update(surveyquestionquestiongroup);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //http://stackoverflow.com/questions/5739362/modelstate-addmodelerror-how-can-i-add-an-error-that-isnt-for-a-property
                    ModelState.AddModelError(string.Empty, "This surveyQuestion - questionGroup combination already exists");
                }

            }
            ViewBag.QuestionGroupFK = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence", surveyquestionquestiongroup.QuestionGroupFK);
            ViewBag.SurveyQuestionFK = new SelectList(unitOfWork.SurveyQuestionRepository.Get(), "Id", "Text", surveyquestionquestiongroup.SurveyQuestionFK);
            return View(surveyquestionquestiongroup);
        }

        //
        // GET: /SurveyQuestionQuestionGroups/Delete/5
 
        public ActionResult Delete(int id)
        {
            SurveyQuestionQuestionGroup surveyquestionquestiongroup =unitOfWork.SurveyQuestionQuestionGroupRepository.GetById(id);
            return View(surveyquestionquestiongroup);
        }

        //
        // POST: /SurveyQuestionQuestionGroups/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            SurveyQuestionQuestionGroup surveyquestionquestiongroup = unitOfWork.SurveyQuestionQuestionGroupRepository.GetById(id);
            unitOfWork.SurveyQuestionQuestionGroupRepository.Delete(surveyquestionquestiongroup);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }
    }
}